/*
* 通过 mutations 间接更新 state 的多个方法的对象
* */
import {
  RECEIVE_INDEX,
  RECEIVE_GOOGS,
  RECEIVE_CATEGORIES,
  RECEIVE_USER_INFO,
  RESET_USER_INFO,
  Add_CARTGOODS,
  RECEIVE_GOODSLIST,
  UPDATE_CARTGOODS,
  REMOVE_CARTGOODS,
  REMOVE_GOODSLIST,
  UPDATE_CARTGOODS_SELECTED
} from "./mutation-types"

import {
  reqIndex,
  reqGoods,
  reqCategories,
  reqUserInfo,
  reqLogout,
  reqCartGoods
} from '../api'

import { qiniuUrl } from '../config'

export default {
  // 异步获取首页数据
  async getIndex ({commit}, callback) {
    // 发送异步请求
    const result = await reqIndex()
    // 提交一个 mutation
    if (result.code === 0) {
      const index = result.data
      commit(RECEIVE_INDEX, {index})
      // 数据更新了, 通知一下组件
      callback && callback(index)
    }
  },
  // 异步获取商品数据
  async getGoods ({commit}, callback) {
    // 发送异步请求
    const result = await reqGoods()
    // 提交一个 mutation
    if (result.code === 0) {
      let goods = result.data
      // 循环遍历修改数据
      goods = goods.map(item => {
        let images = item.images
        let newImages = images.map(item => {
          return item = qiniuUrl + item
        })
        return {
          id: item._id,
          title: item.title,
          price: item.price,
          inventory_count: item.inventory_count,
          pics: newImages,
          createTime: item.createTime
        }
      })
      commit(RECEIVE_GOOGS, {goods})
      // 数据更新了, 通知一下组件
      callback && callback()
    }
  },
  // 异步获取分类数据
  async getCategories ({commit}, callback) {
    // 发送异步请求
    const result = await reqCategories()
    // 提交一个 mutation
    if (result.code === 0) {
      const categories = result.data
      commit(RECEIVE_CATEGORIES, {categories})
      // 数据更新了, 通知一下组件
      callback && callback()
    }
  },
  // 同步记录用户信息
  recordUser ({commit}, userInfo) {
    commit(RECEIVE_USER_INFO, {userInfo})
  },
  // 异步获取用户信息
  async getUserInfo ({commit}, callback) {
    const result = await reqUserInfo()
    if (result.code === 0) {
      const userInfo = await result.data
      console.log(userInfo)
      commit(RECEIVE_USER_INFO, {userInfo})
      callback && callback()
    }
  },
  // 异步登出
  async logout ({commit}) {
    const result = await reqLogout()
    if (result.code === 0) {
      commit(RESET_USER_INFO)
    }
  },
  // 同步添加到购物车数据
  addToCartGoods ({commit}, goodsInfo)　{
    commit(Add_CARTGOODS, {goodsInfo})
  },
  // 异步获取购物车数据
  async getCartGoods ({commit}, ids, callback) {
    const result = await reqCartGoods(ids)
    if (result.code === 0) {
      let goodsList = result.data
      if (goodsList.length) {
        // 循环遍历修改数据
        goodsList.forEach(item => {
          item.pic = qiniuUrl + item.pic
        })
      }
      // console.log(goodsList)
      commit(RECEIVE_GOODSLIST, {goodsList})
      callback && callback()
    }
  },
  // 同步更新购物车的数据
  updateCartGoods ({commit}, goodsInfo) {
    commit(UPDATE_CARTGOODS, {goodsInfo})
  },
  // 同步删除购物车中的数据
  removeCartGoods ({commit, state}, id) {
    commit(REMOVE_CARTGOODS, {id})
  },
  // 同步删除商品列表中的数据
  removeGoodsList ({commit, state}, index) {
    commit(REMOVE_GOODSLIST, {index})
  },
  // 更新购物车中的selected
  updateCartGoodsSelected ({commit}, {id, val}) {
    commit(UPDATE_CARTGOODS_SELECTED,{id, val})
  }
}
